package com.zw.yzk.kabu.view.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.adapter.TicketPropertyAdapter;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.model.ExhibitionDetailsModel;
import com.zw.yzk.kabu.utils.ResUtils;

/**
 * Created by zhanwei on 2017/2/5.
 */

public class BuyTicketDialog {
    private static class Params {
        public PopupWindow popupWindow;
        public Context context;
        public TicketPropertyAdapter adapter;

        public String deadLine;
        public String price;
        public int stock;
        public int limit;
        public String image;
        public int count;
        public ExhibitionDetailsModel model;
        public OnConfirmClickedListener listener;

        public Params() {
            count = 1;
        }
    }

    public interface OnConfirmClickedListener {
        void onClick(int count, ExhibitionDetailsModel.Property property);
    }

    public static class Builder {
        private final Params P;

        public Builder(Context context) {
            P = new Params();
            P.context = context;
        }

        public Builder setImage(String image) {
            P.image = image;
            return this;
        }

        public Builder setDeadLine(String deadLine) {
            P.deadLine = deadLine;
            return this;
        }

        public Builder setConfirmListener(OnConfirmClickedListener listener) {
            P.listener = listener;
            return this;
        }

        public Builder setPropertyList(ExhibitionDetailsModel model) {
            P.model = model;
            P.adapter = new TicketPropertyAdapter(model.getPropertyList());
            return this;
        }

        public PopupWindow build() {
            View rootView = LayoutInflater.from(P.context).inflate(R.layout.dialog_buy_ticket, null);
            ImageView image = (ImageView) rootView.findViewById(R.id.image);
            final TextView price = (TextView) rootView.findViewById(R.id.price);
            final TextView stock = (TextView) rootView.findViewById(R.id.stock);
            TextView deadline = (TextView) rootView.findViewById(R.id.deadline);

            RecyclerView property = (RecyclerView) rootView.findViewById(R.id.property);
            final TextView count = (TextView) rootView.findViewById(R.id.count);
            View decrease = rootView.findViewById(R.id.decrease);
            View increase = rootView.findViewById(R.id.increase);
            View confirm = rootView.findViewById(R.id.confirm);
            View cancel = rootView.findViewById(R.id.cancel);
            final TextView maxBuyCount = (TextView) rootView.findViewById(R.id.max_buy_count);

            deadline.setText(P.context.getResources().getString(R.string.deadline, P.deadLine));
            price.setText(ResUtils.formatCurrency(P.context, P.adapter.getDataList().get(0).getOriginalPrice()));

            P.stock = P.adapter.getDataList().get(0).getStock();
            P.limit = P.adapter.getDataList().get(0).getLimit();
            maxBuyCount.setText(P.context.getResources().getString(R.string.max_buy_ticker_count, P.limit));
            stock.setText(P.context.getResources().getString(R.string.ticket_stock, P.stock));
            if (P.stock == 0) {
                P.count = 0;
            }
            count.setText(String.valueOf(P.count));
            property.setLayoutManager(new LinearLayoutManager(P.context, LinearLayoutManager.VERTICAL, false));
            property.setAdapter(P.adapter);

            Glide.with(P.context)
                    .load(P.image)
                    .centerCrop()
                    .error(R.drawable.im_default)
                    .into(image);

            decrease.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (P.count <= 1) {
                        ToastManager.getInstance().showToast(P.context, P.context.getResources().getString(R.string.could_not_decrease));
                        return;
                    }
                    P.count--;
                    count.setText(String.valueOf(P.count));
                    price.setText(ResUtils.formatCurrency(P.context, P.adapter.getDataList().get(0).getOriginalPrice() * P.count));
                }
            });
            increase.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (P.count >= P.limit) {
                        ToastManager.getInstance().showToast(P.context, P.context.getResources().getString(R.string.could_not_increase, P.limit));
                        return;
                    }
                    P.count++;
                    count.setText(String.valueOf(P.count));
                    price.setText(ResUtils.formatCurrency(P.context, P.adapter.getDataList().get(0).getOriginalPrice() * P.count));
                }
            });
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (P.popupWindow == null) {
                        return;
                    }
                    P.popupWindow.dismiss();
                }
            });
            confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (P.popupWindow == null) {
                        return;
                    }
                    if (P.listener != null) {
                        P.listener.onClick(P.count, P.adapter.getSelectedProperty());
                    }
                    P.popupWindow.dismiss();
                }
            });
            P.adapter.setOnPropertySelectedListener(new TicketPropertyAdapter.OnPropertySelectedListener() {
                @Override
                public void onClicked(ExhibitionDetailsModel.Property property) {
                    price.setText(ResUtils.formatCurrency(P.context, property.getOriginalPrice() * P.count));
                    P.stock = property.getStock();
                    P.limit = property.getLimit();
                    stock.setText(P.context.getResources().getString(R.string.ticket_stock, P.stock));
                    maxBuyCount.setText(P.context.getResources().getString(R.string.max_buy_ticker_count, P.limit));
                }
            });


            //加载对话框布局
            P.popupWindow = new PopupWindow(rootView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            P.popupWindow.setAnimationStyle(R.style.dialog_bottom_in_out_style);

            //设置点击popupwindow外消失
            P.popupWindow.setBackgroundDrawable(new ColorDrawable());
            P.popupWindow.setOutsideTouchable(true);
            P.popupWindow.setFocusable(true);

            return P.popupWindow;
        }
    }
}
